{% extends "base.html" %}

{% block content %}
<!-- 加载遮罩 -->
<div class="loading-overlay">
    <div class="loading-spinner">
        <i class="fas fa-spinner fa-spin"></i>
        <p>正在处理图片，请稍候...</p>
    </div>
</div>

<!-- PC端侧边栏导航 -->
<div class="d-none d-md-flex sidebar">
    <div class="nav flex-column nav-pills me-3" role="tablist">
        <button class="nav-link active" data-bs-toggle="pill" data-bs-target="#daily-plan" type="button">
            <i class="fas fa-calendar-day"></i> 今日学习
        </button>
        <button class="nav-link" data-bs-toggle="pill" data-bs-target="#word-management" type="button">
            <i class="fas fa-book"></i> 词库管理
        </button>
        <button class="nav-link" data-bs-toggle="pill" data-bs-target="#statistics" type="button">
            <i class="fas fa-chart-bar"></i> 学习统计
        </button>
        <button class="nav-link" data-bs-toggle="pill" data-bs-target="#homework" type="button">
            <i class="fas fa-tasks"></i> 作业管理
        </button>
        <button class="nav-link" data-bs-toggle="pill" data-bs-target="#add-word" type="button">
            <i class="fas fa-plus"></i> 添加单词
        </button>
    </div>
</div>

<!-- 主要内容区域 -->
<div class="tab-content main-content">
    {% with messages = get_flashed_messages(with_categories=true) %}
        {% if messages %}
            <div class="alert alert-dismissible fade show" role="alert">
                {% for category, message in messages %}
                    <div class="alert alert-{{ category }}">{{ message }}</div>
                {% endfor %}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        {% endif %}
    {% endwith %}
    <!-- 今日学习计划 -->
    <div class="tab-pane fade show active" id="daily-plan">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">今日学习计划</h5>
                {% if daily_words %}
                    <div class="row">
                    {% for word in daily_words %}
                        <div class="col-md-6 col-lg-4 mb-3">
                            <div class="card h-100">
                                <div class="card-body">
                                    <h5 class="card-title">{{ word.word }}</h5>
                                    <p class="card-text">
                                        <small class="text-muted">{{ word.pronunciation }}</small>
                                        {% if word.audio_url %}
                                            <button class="btn btn-sm btn-outline-primary play-audio" 
                                                    data-audio="{{ word.audio_url }}">
                                                <i class="fas fa-volume-up"></i>
                                            </button>
                                        {% endif %}
                                    </p>
                                    <p class="card-text text-primary">{{ word.translation }}</p>
                                    <p class="card-text">{{ word.example }}</p>
                                    <p class="text-muted">
                                        <small>第 {{ word.review_count + 1 }} 次复习 
                                        {% if word.review_count >= 6 %}
                                            (最后一次复习)
                                        {% endif %}
                                        </small>
                                    </p>
                                    {% if word.review_count >= 6 %}
                                        <div class="btn-group w-100" role="group">
                                            <button class="btn btn-success btn-sm review-btn" 
                                                    data-word-id="{{ word.id }}"
                                                    data-mastered="true">
                                                已掌握
                                            </button>
                                            <button class="btn btn-warning btn-sm review-btn" 
                                                    data-word-id="{{ word.id }}"
                                                    data-mastered="false">
                                                需要继续复习
                                            </button>
                                        </div>
                                    {% else %}
                                        <button class="btn btn-primary btn-sm review-btn w-100" 
                                                data-word-id="{{ word.id }}"
                                                data-mastered="false">
                                            标记为已复习
                                        </button>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    {% endfor %}
                    </div>
                {% else %}
                    <p class="text-muted">今天没有需要复习的单词</p>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- 词库管理 -->
    <div class="tab-pane fade" id="word-management">
        <div class="row">
            <!-- 上传图片区域 -->
            <div class="col-md-6">
                <div class="card">
                    <div class="card-body">
                        <h5 class="card-title">上传图片</h5>
                        <form method="POST" enctype="multipart/form-data" id="uploadForm">
                            <div class="mb-3">
                                <label for="file" class="form-label">选择图片文件</label>
                                <input type="file" class="form-control" id="file" name="file" accept="image/*">
                            </div>
                            <button type="submit" class="btn btn-primary">上传并识别</button>
                        </form>
                        <div class="mt-3">
                            <a href="{{ url_for('main.init_database') }}" 
                               class="btn btn-warning"
                               onclick="return confirm('是否添加示例数据？这只会在数据库为空时添加示例内容。')">
                                <i class="fas fa-database me-1"></i>
                                添加示例数据
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- 单词列表区域 -->
            <div class="col-md-6">
                <div class="card">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center mb-3">
                            <h5 class="card-title mb-0">已保存的单词</h5>
                            <div class="btn-group">
                                <button type="button" class="btn btn-outline-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                                    导出词库
                                </button>
                                <ul class="dropdown-menu">
                                    <li><a class="dropdown-item" href="{{ url_for('main.export_words', format='csv') }}">导出为 CSV</a></li>
                                    <li><a class="dropdown-item" href="{{ url_for('main.export_words', format='excel') }}">导出为 Excel</a></li>
                                </ul>
                            </div>
                        </div>
                        
                        {% if words %}
                            <div class="list-group word-list">
                            {% for word in words %}
                                <div class="list-group-item">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div>
                                            <strong>{{ word.word }}</strong>
                                            <small class="text-muted">{{ word.pronunciation }}</small>
                                            {% if word.audio_url %}
                                                <button class="btn btn-sm btn-outline-primary play-audio" 
                                                        data-audio="{{ word.audio_url }}">
                                                    <i class="fas fa-volume-up"></i>
                                                </button>
                                            {% endif %}
                                        </div>
                                        <small class="text-muted">
                                            {{ word.created_at.strftime('%Y-%m-%d') }}
                                        </small>
                                    </div>
                                    <p class="card-text text-primary mb-1">{{ word.translation }}</p>
                                    {% if word.example %}
                                        <small class="d-block mt-1">{{ word.example }}</small>
                                    {% endif %}
                                </div>
                            {% endfor %}
                            </div>
                        {% else %}
                            <p class="text-muted">还没有保存任何单词</p>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 学习统计 -->
    <div class="tab-pane fade" id="statistics">
        <div class="statistics-container">
            <!-- 概览卡片 -->
            <div class="row mb-4">
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-body text-center">
                            <h5 class="card-title">总单词量</h5>
                            <h2 class="text-primary">{{ stats.total_words }}</h2>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-body text-center">
                            <h5 class="card-title">已掌握</h5>
                            <h2 class="text-success">{{ stats.mastered_words }}</h2>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-body text-center">
                            <h5 class="card-title">学习中</h5>
                            <h2 class="text-warning">{{ stats.in_progress }}</h2>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 图表区域 -->
            <div class="row">
                <!-- 每日新增单词趋势 -->
                <div class="col-md-6 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">每日新增单词</h5>
                            <canvas id="dailyWordsChart"></canvas>
                        </div>
                    </div>
                </div>
                
                <!-- 复习次数分布 -->
                <div class="col-md-6 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">复习次数分布</h5>
                            <canvas id="reviewDistributionChart"></canvas>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 积分历史 -->
            <div class="row">
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">
                                <i class="fas fa-history me-2"></i>
                                最近获得的积分
                            </h5>
                            <!-- 添加手动添加积分的表单 -->
                            <form id="add-points-form" class="mb-3">
                                <div class="input-group">
                                    <input type="number" class="form-control" name="points" 
                                           placeholder="输入积分" min="1" required>
                                    <input type="text" class="form-control" name="reason" 
                                           placeholder="添加原因" required>
                                    <button class="btn btn-primary" type="submit">
                                        <i class="fas fa-plus"></i> 添加积分
                                    </button>
                                </div>
                            </form>
                            <!-- 添加初始化积分按钮 -->
                            <div class="text-end mb-3">
                                <button onclick="resetPoints()" class="btn btn-danger btn-sm">
                                    <i class="fas fa-trash"></i> 初始化积分
                                </button>
                            </div>
                            <div class="list-group">
                                {% for record in recent_points %}
                                <div class="list-group-item">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div>
                                            <small class="text-muted">
                                                {{ record.created_at.strftime('%Y-%m-%d %H:%M') }}
                                            </small>
                                            <div>{{ record.reason }}</div>
                                        </div>
                                        <span class="badge bg-primary">+{{ record.points }}</span>
                                    </div>
                                </div>
                                {% endfor %}
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- 连续登录信息 -->
                <div class="col-md-6">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">
                                <i class="fas fa-calendar-check me-2"></i>
                                连续学习
                            </h5>
                            <div class="text-center">
                                <h2 class="mb-3">{{ user.consecutive_days }} 天</h2>
                                <p class="text-muted">
                                    连续学习天数越多，每日登录奖励越多！
                                    <br>
                                    最高可获得每日 30 积分
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 作业管理 -->
    <div class="tab-pane fade" id="homework">
        <div class="row">
            <!-- 作业提交表单 -->
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">记录今日作业</h5>
                    </div>
                    <div class="card-body">
                        <form id="homework-form">
                            <div class="mb-3">
                                <label class="form-label">科目</label>
                                <select class="form-select" name="subject" required>
                                    <option value="">选择科目</option>
                                    <option value="语文">语文</option>
                                    <option value="数学">数学</option>
                                    <option value="英语">英语</option>
                                </select>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">作业内容</label>
                                <textarea class="form-control" name="content" rows="3" required></textarea>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">耗时（小时）</label>
                                <input type="range" class="form-range" name="hours" min="0" max="10" value="0" id="hoursRange">
                                <div class="text-center" id="hoursValue">0 小时</div>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">耗时（分钟）</label>
                                <input type="range" class="form-range" name="minutes" min="0" max="59" value="0" id="minutesRange">
                                <div class="text-center" id="minutesValue">0 分钟</div>
                            </div>
                            
                            <button type="submit" class="btn btn-primary mt-3">提交作业</button>
                        </form>
                    </div>
                </div>
            </div>
            
            <!-- 学习时间统计 -->
            <div class="col-md-6">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">近三天学习时间</h5>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            {% for subject, minutes in subject_times.items() %}
                            <div class="col-4 text-center">
                                <h4>{{ subject }}</h4>
                                <p class="text-muted">
                                    {{ minutes // 60 }}小时 
                                    {{ minutes % 60 }}分钟
                                </p>
                            </div>
                            {% endfor %}
                        </div>
                    </div>
                </div>
                
                <!-- 最近作业记录 -->
                <div class="card mt-3">
                    <div class="card-header">
                        <h5 class="card-title mb-0">最近作业记录</h5>
                    </div>
                    <ul class="list-group list-group-flush">
                        {% for hw in recent_homework %}
                        <li class="list-group-item">
                            <div class="d-flex justify-content-between">
                                <div>
                                    <strong>{{ hw.subject }}</strong>
                                    <small class="text-muted d-block">{{ hw.content }}</small>
                                </div>
                                <div class="text-end">
                                    <span>{{ hw.hours }}小时 {{ hw.minutes }}分钟</span>
                                    <small class="text-muted d-block">
                                        {{ hw.created_at.strftime('%Y-%m-%d %H:%M') }}
                                    </small>
                                </div>
                            </div>
                        </li>
                        {% endfor %}
                    </ul>
                </div>
            </div>
        </div>
    </div>

    <!-- 添加单词 -->
    <div class="tab-pane fade" id="add-word">
        <h2>手动添加单词</h2>
        <form id="add-word-form">
            <div class="mb-3">
                <label for="word" class="form-label">输入单词</label>
                <input type="text" class="form-control" id="word" name="word" required>
            </div>
            <div class="mb-3">
                <label for="example" class="form-label">例句（可选）</label>
                <textarea class="form-control" id="example" name="example" rows="2" 
                          placeholder="如果不填写，将自动获取例句"></textarea>
            </div>
            <button type="submit" class="btn btn-primary">提交</button>
        </form>
        <div id="message" class="mt-3"></div>
    </div>
</div>

<!-- 移动端底部导航 -->
<nav class="mobile-nav d-md-none">
    <div class="nav nav-pills nav-fill" role="tablist">
        <button class="nav-link active" data-bs-toggle="pill" data-bs-target="#daily-plan" type="button">
            <i class="fas fa-calendar-day"></i>
            <span>今日学习</span>
        </button>
        <button class="nav-link" data-bs-toggle="pill" data-bs-target="#word-management" type="button">
            <i class="fas fa-book"></i>
            <span>词库管理</span>
        </button>
        <button class="nav-link" data-bs-toggle="pill" data-bs-target="#statistics" type="button">
            <i class="fas fa-chart-bar"></i>
            <span>学习统计</span>
        </button>
        <button class="nav-link" data-bs-toggle="pill" data-bs-target="#homework" type="button">
            <i class="fas fa-tasks"></i>
            <span>作业</span>
        </button>
        <button class="nav-link" data-bs-toggle="pill" data-bs-target="#add-word" type="button">
            <i class="fas fa-plus"></i> 添加单词
        </button>
    </div>
</nav>

<!-- 添加积分通知模态框 -->
<div class="modal fade" id="pointsModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-body text-center">
                <i class="fas fa-star text-warning fa-3x mb-3"></i>
                <h5 class="points-message"></h5>
                <div class="points-animation">+<span></span></div>
            </div>
        </div>
    </div>
</div>

<!-- 添加JavaScript -->
{% block scripts %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    const loadingOverlay = document.querySelector('.loading-overlay');
    const uploadForm = document.getElementById('uploadForm');

    // 处理表单提交
    uploadForm.addEventListener('submit', function() {
        // 检查是否选择了文件
        const fileInput = document.getElementById('file');
        if (fileInput.files.length > 0) {
            // 显示加载遮罩
            loadingOverlay.style.display = 'block';
            
            // 禁用提交按钮
            const submitButton = this.querySelector('button[type="submit"]');
            submitButton.disabled = true;
        }
    });

    // 播放音频
    document.querySelectorAll('.play-audio').forEach(button => {
        button.addEventListener('click', function() {
            const audio = new Audio(this.dataset.audio);
            audio.play();
        });
    });

    // 更新标记复习的代码
    document.querySelectorAll('.review-btn').forEach(button => {
        button.addEventListener('click', function() {
            const wordId = this.dataset.wordId;
            const mastered = this.dataset.mastered === 'true';
            
            fetch(`/review/${wordId}?_=${new Date().getTime()}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Cache-Control': 'no-cache'
                },
                body: JSON.stringify({ mastered: mastered })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    // 找到并移除整个卡片
                    const card = this.closest('.col-md-6');
                    if (card) {
                        card.remove();
                    }
                }
            });
        });
    });

    // 当切换到统计tab时初始化图表
    document.querySelector('button[data-bs-target="#statistics"]').addEventListener('shown.bs.tab', function (e) {
        // 每日新增单词趋势图
        const dailyWordsCtx = document.getElementById('dailyWordsChart').getContext('2d');
        new Chart(dailyWordsCtx, {
            type: 'line',
            data: {
                labels: [
                    {% for date, count in stats.daily_new_words %}
                        '{{ date }}',
                    {% endfor %}
                ],
                datasets: [{
                    label: '新增单词数',
                    data: [
                        {% for date, count in stats.daily_new_words %}
                            {{ count }},
                        {% endfor %}
                    ],
                    borderColor: '#3498db',
                    tension: 0.1,
                    fill: false
                }]
            },
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        position: 'top',
                    }
                },
                scales: {
                    y: {
                        beginAtZero: true,
                        ticks: {
                            stepSize: 1
                        }
                    }
                }
            }
        });

        // 复习次数分布图
        const reviewDistCtx = document.getElementById('reviewDistributionChart').getContext('2d');
        new Chart(reviewDistCtx, {
            type: 'bar',
            data: {
                labels: [
                    {% for review_count, count in stats.review_distribution %}
                        '{{ review_count }}次',
                    {% endfor %}
                ],
                datasets: [{
                    label: '单词数量',
                    data: [
                        {% for review_count, count in stats.review_distribution %}
                            {{ count }},
                        {% endfor %}
                    ],
                    backgroundColor: '#2ecc71'
                }]
            },
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        position: 'top',
                    }
                },
                scales: {
                    y: {
                        beginAtZero: true,
                        ticks: {
                            stepSize: 1
                        }
                    }
                }
            }
        });
    });

    // 处理作业进度更新
    document.querySelectorAll('.update-progress').forEach(button => {
        button.addEventListener('click', function() {
            const homeworkId = this.dataset.homeworkId;
            
            fetch(`/update_homework_progress/${homeworkId}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'Cache-Control': 'no-cache'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    // 刷新页面以显示更新后的进度
                    location.reload();
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('更新进度失败，请重试');
            });
        });
    });

    // 更新滑块值显示
    const hoursRange = document.getElementById('hoursRange');
    const hoursValue = document.getElementById('hoursValue');
    hoursRange?.addEventListener('input', function() {
        hoursValue.textContent = this.value + ' 小时';
    });

    const minutesRange = document.getElementById('minutesRange');
    const minutesValue = document.getElementById('minutesValue');
    minutesRange?.addEventListener('input', function() {
        minutesValue.textContent = this.value + ' 分钟';
    });

    // 处理手动添加积分
    var isAddingPoints = false;
    const addPointsForm = document.getElementById('add-points-form');
    addPointsForm?.addEventListener('submit', function(e) {
        e.preventDefault();
        
        if (isAddingPoints) return;
        isAddingPoints = true;
        
        const formData = new FormData(this);
        
        fetch('/add_points', {
            method: 'POST',
            body: formData
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                // 显示积分奖励通知
                showPointsNotification({
                    points: data.points,
                    message: data.message
                });
                // 清空表单
                this.reset();
            } else {
                alert(data.message);
            }
        })
        .catch(error => {
            console.error('Error:', error);
            alert('添加积分失败，请重试');
        })
        .finally(() => {
            isAddingPoints = false;
        });
    });
});

// 提交作业的处理
var homework_submit = false;
const homeworkForm = document.getElementById('homework-form');
homeworkForm?.addEventListener('submit', function(e) {
    e.preventDefault();
    if (homework_submit) return;
    homework_submit = true;

    const formData = new FormData(this);
    
    fetch("/homework", {
        method: 'POST',
        headers: {
            'X-Requested-With': 'XMLHttpRequest'
        },
        body: formData
    })
    .then(response => {
        if (!response.ok) {
            throw new Error('网络响应不正常');
        }
        return response.json();
    })
    .then(data => {
        if (data.success) {
            // 显示积分奖励
            showPointsNotification({
                points: data.points_earned,
                message: `完成作业获得 ${data.points_earned} 积分`
            });
            
            // 清空表单
            this.reset();
            // 重新加载作业列表（可以通过AJAX获取新数据）
            loadHomeworkList();
        } else {
            alert('提交作业失败，请重试。');
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('提交作业失败，请重试。');
    })
    .finally(() => {
        homework_submit = false;
    });
});

// 添加加载作业列表的函数
function loadHomeworkList() {
    fetch('/get_homework_list')
        .then(response => response.json())
        .then(data => {
            // 更新作业列表和统计数据
            // TODO: 实现更新DOM的逻辑
        });
}

// 显示积分奖励通知
function showPointsNotification(data) {
    const modal = new bootstrap.Modal(document.getElementById('pointsModal'));
    const messageEl = document.querySelector('#pointsModal .points-message');
    const pointsEl = document.querySelector('#pointsModal .points-animation span');
    const totalPointsEl = document.querySelector('.points-value');
    const loadingOverlay = document.querySelector('.loading-overlay');
    
    messageEl.textContent = data.message;
    pointsEl.textContent = data.points;
    
    // 确保遮罩层隐藏
    loadingOverlay.style.display = 'none';
    
    modal.show();
    
    // 更新总积分显示
    totalPointsEl.textContent = parseInt(totalPointsEl.textContent) + data.points;
    
    // 3秒后自动关闭模态框
    setTimeout(() => {
        modal.hide();
        // 确保遮罩层隐藏
        loadingOverlay.style.display = 'none';
    }, 3000);
    
    // 监听模态框隐藏事件
    document.getElementById('pointsModal').addEventListener('hidden.bs.modal', function () {
        // 确保遮罩层隐藏
        loadingOverlay.style.display = 'none';
    });
}

// 检查每日登录奖励
document.addEventListener('DOMContentLoaded', function() {
    {% if daily_points %}
        showPointsNotification({
            points: {{ daily_points.points }},
            message: `{{ daily_points.message|safe }}`
        });
    {% endif %}
});

var isSubmitting = false;

document.getElementById('add-word-form').addEventListener('submit', function(e) {
    e.preventDefault();
    console.log("add word submit......")
    
    if (isSubmitting) return;  // 如果正在提交，则返回
    isSubmitting = true;  // 设置为正在提交
    // 清空消息
    const messageDiv = document.getElementById('message');
    messageDiv.innerHTML = '<div class="alert alert-danger">正在添加单词......</div>';

    const formData = new FormData(this);
    
    fetch("/add_word", {
        method: 'POST',
        body: formData
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            messageDiv.innerHTML = '<div class="alert alert-success">' + data.message + '</div>';
            document.getElementById('add-word-form').reset();  // 清空表单
        } else {
            messageDiv.innerHTML = '<div class="alert alert-danger">' + data.message + '</div>';
        }
    })
    .catch(error => {
        console.error('Error:', error);
        document.getElementById('message').innerHTML = '<div class="alert alert-danger">添加单词失败，请重试。</div>';
    })
    .finally(() => {
        isSubmitting = false;  // 在请求完成后重置状态
    });
});

function resetPoints() {
    if (confirm('确定要初始化所有用户的积分吗？此操作不可撤销！')) {
        fetch('/reset_points', {
            method: 'POST',
        })
        .then(response => response.json())
        .then(data => {
            if (data.status === 'success') {
                alert('积分已成功重置');
                location.reload(); // 刷新页面以显示更新后的积分
            } else {
                alert('初始化失败：' + data.message);
            }
        })
        .catch(error => {
            alert('发生错误：' + error);
        });
    }
}
</script>
{% endblock %}
{% endblock %} 